package com.theobserver.theobserver;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;

public class LogInActivity extends AppCompatActivity {


    CheckBox savePwdCB;
    SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);




       //记住密码准备工作
        sp = this.getSharedPreferences("userInfo", MODE_PRIVATE);
        savePwdCB = (CheckBox) findViewById(R.id.CB_SavePwad);
        final EditText username = (EditText) findViewById(R.id.et_login_user);
        final EditText userpwd = (EditText) findViewById(R.id.et_login_pwd);
        //判断记住密码框的状态
        if(sp.getBoolean("ISCHECK", false))
        {
            //设置默认是记录密码状态
            savePwdCB.setChecked(true);
            username.setText(sp.getString("USER_NAME", ""));
            userpwd.setText(sp.getString("PASSWORD", ""));
        }



        //登录
        Button bt1 = (Button) findViewById(R.id.btn_Login_ok);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String name = username.getText().toString();
                final String pwd = userpwd.getText().toString();


                if (name.length() != 0) {
                    if (pwd.length() != 0) {
                        //验证登录信息
                        AVUser.logInInBackground(name, pwd, new LogInCallback<AVUser>() {
                            @Override
                            public void done(AVUser avUser, AVException e) {
                                if (e == null) {
                                    Toast.makeText(LogInActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(LogInActivity.this,UserCenterActivity.class));


                                    if(savePwdCB.isChecked())
                                    {
                                        //记住用户名、密码、
                                        SharedPreferences.Editor editor = sp.edit();
                                        editor.putString("USER_NAME", name);
                                        editor.putString("PASSWORD",pwd);
                                        editor.commit();
                                    }
                                    setResult(Code.TRUE,null);

                                    LogInActivity.this.finish();
                                } else if (e.getCode() == 211) {
                                    Toast.makeText(LogInActivity.this, "该邮箱未注册!", Toast.LENGTH_SHORT).show();
                                } else if (e.getCode() == 210) {
                                    Toast.makeText(LogInActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
                                }
                                else if (e.getCode()==216){
                                    Toast.makeText(LogInActivity.this, "请前往邮箱进行验证!", Toast.LENGTH_SHORT).show();
                                }
                                else if (e.getCode()==219){
                                    Toast.makeText(LogInActivity.this, "登录失败次数超过限制，请稍候再试，或者通过忘记密码重设密码！", Toast.LENGTH_SHORT).show();
                                }
                                else
                                    Toast.makeText(LogInActivity.this,   e.toString(), Toast.LENGTH_SHORT).show();
                                //或者Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        Toast.makeText(LogInActivity.this, "请输入密码!", Toast.LENGTH_SHORT).show();
                    }
                } else
                    Toast.makeText(LogInActivity.this, "请输入邮箱账号!", Toast.LENGTH_SHORT).show();
            }
        });

        //记住密码多选框
        savePwdCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (savePwdCB.isChecked()) {

                    Toast.makeText(LogInActivity.this, "记住密码已选中!", Toast.LENGTH_SHORT).show();
                    sp.edit().putBoolean("ISCHECK", true).commit();

                }else {
                    Toast.makeText(LogInActivity.this, "记住密码未选中!", Toast.LENGTH_SHORT).show();
                    sp.edit().putBoolean("ISCHECK", false).commit();

                }

            }
        });




        //忘记密码
        Button bt2=(Button)findViewById(R.id.btn_FgetPwd) ;
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LogInActivity.this,FgetPwdActivity.class));
            }
        });


        //注册
        TextView Tv=(TextView) findViewById(R.id.tv_Reg) ;
        Tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LogInActivity.this,SignInActivity.class));
            }
        });

        //返回按钮
        TextView tv_reten=(TextView) findViewById(R.id.tv_Login_reten);
        tv_reten.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setResult(Code.FALSE,null);

                finish();
            }
        });

    }

}
